[id].vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. <template>
  2. <div
  3. class="max-w-screen-xl content my-0 mx-auto text-size-base overflow-hidden"
  4. :class="!detail ? 'flex flex-column justify-center items-center h-full' : ''"
  5. >
  6. <div v-if="detail">
  7. <div class="breadcrumb my-[30px] pb-[20px] bb-1px_#EBEBEB" role="navigation">
  8. <n-breadcrumb separator=">" v-if="breadcrumb">
  9. <n-breadcrumb-item @click="$router.push('/')">{{ $t('home') }}</n-breadcrumb-item>
  10. <template v-for="(bread, index) in breadcrumb" :key="index">
  11. <n-breadcrumb-item @click="handleBreadcrumb(bread)">
  12. {{ bread.title }}</n-breadcrumb-item
  13. >
  14. </template>
  15. </n-breadcrumb>
  16. </div>
  17. <div v-if="currentCate">
  18. <n-h2 class="doc-title mb-10"> {{ currentCate.title }}</n-h2>
  19. </div>
  20. <div class="waper-content w-full flex flex-row flex-nowrap">
  21. <div
  22. class="waper-menu flex-basis-[240px] flex-grow-0 flex-shrink-0 br-1px_#EBEBEB overflow-hidden"
  23. :class="{ open: isOpenMuen }"
  24. >
  25. <div class="open-btn" @click="toggleOpen">
  26. <svg
  27. xmlns="http://www.w3.org/2000/svg"
  28. xmlns:xlink="http://www.w3.org/1999/xlink"
  29. viewBox="0 0 512 512"
  30. >
  31. <path
  32. fill="none"
  33. stroke="currentColor"
  34. stroke-linecap="round"
  35. stroke-linejoin="round"
  36. stroke-width="32"
  37. d="M96 256h320"
  38. ></path>
  39. <path
  40. fill="none"
  41. stroke="currentColor"
  42. stroke-linecap="round"
  43. stroke-linejoin="round"
  44. stroke-width="32"
  45. d="M96 176h320"
  46. ></path>
  47. <path
  48. fill="none"
  49. stroke="currentColor"
  50. stroke-linecap="round"
  51. stroke-linejoin="round"
  52. stroke-width="32"
  53. d="M96 336h320"
  54. ></path>
  55. </svg>
  56. </div>
  57. <n-tree
  58. class="left-tree"
  59. v-model:selected-keys="selectedKeys"
  60. v-if="mainCategories"
  61. block-line
  62. :data="mainCategories.children"
  63. :default-expanded-keys="defaultExpandedKeys"
  64. :node-props="nodeProps"
  65. key-field="id"
  66. label-field="title"
  67. children-field="children"
  68. selectable
  69. >
  70. <template #empty>
  71. <n-empty :description="$t('no_data')" />
  72. </template>
  73. </n-tree>
  74. </div>
  75. <div class="infos flex-1 pages w-[calc(100%-80px)] px-[40px] mb-[80px] overflow-hidden">
  76. <div class="box bb-1px_#EBEBEB color-[#999999]">
  77. <n-h2 class="font-700"> {{ detail.title }}</n-h2>
  78. <span class="flex flex-row gap-col-6 pb-[15px]">
  79. <p class="time">
  80. {{ $t('publish') }} {{ dayjs(detail.createTime).format('YYYY-MM-DD') }}
  81. </p>
  82. <!-- <p>{{ $t('read') }} ( {{ detail.readCount }} )</p> -->
  83. </span>
  84. </div>
  85. <div class="w-full content-html" v-html="detail.content"></div>
  86. <div class="top-clt w-full flex justify-between pt-6 bt-1px_#EBEBEB color-[#999898]">
  87. <div class="prev flex flex-col">
  88. <span
  89. class="flex-1 text-align-left"
  90. :class="{ articleNear: articleNear[0] }"
  91. @click="handleToArticle(articleNear[0])"
  92. >
  93. {{ $t('prev_article') }}
  94. </span>
  95. <span
  96. class="flex-1 articleNear"
  97. v-if="articleNear[0]"
  98. @click="handleToArticle(articleNear[0])"
  99. >
  100. {{ articleNear[0].title }}
  101. </span>
  102. <span v-else>
  103. {{ $t('no_data') }}
  104. </span>
  105. </div>
  106. <div class="next flex flex-col">
  107. <span
  108. class="flex-1 text-align-right"
  109. :class="{ articleNear: articleNear[1] }"
  110. @click="handleToArticle(articleNear[1])"
  111. >
  112. {{ $t('next_article') }}
  113. </span>
  114. <span
  115. class="flex-1 articleNear"
  116. v-if="articleNear[1]"
  117. @click="handleToArticle(articleNear[1])"
  118. >
  119. {{ articleNear[1].title }}
  120. </span>
  121. <span v-else>
  122. {{ $t('no_data') }}
  123. </span>
  124. </div>
  125. </div>
  126. <n-back-top to=".pages" :visibility-height="800" />
  127. </div>
  128. <div class="desc-list flex-basis-[240px] flex-grow-0 flex-shrink-0 overflow-hidden">
  129. <div class="min-h-[200px] bg-[#F5F9FF] b-r-[8px] p-[20px]">
  130. <n-h4 class="font-600 ext-size-base">{{ $t('main_content') }}</n-h4>
  131. <n-anchor :show-rail="false">
  132. <n-anchor-link
  133. class="text-size-base color-[#999999]"
  134. v-for="(item, index) in mainContents"
  135. :key="index"
  136. :title="item.text"
  137. :href="`#my-section-${index + 1}`"
  138. >
  139. </n-anchor-link>
  140. </n-anchor>
  141. </div>
  142. </div>
  143. </div>
  144. </div>
  145. <div v-else>
  146. <n-empty :description="$t('no_article')">
  147. <template #extra>
  148. {{ $t('no_data_article') }},
  149. <n-button link size="small" quaternary type="primary" @click="router.push('/')">
  150. {{ $t('go_home') }}
  151. </n-button>
  152. </template>
  153. <template #default></template>
  154. </n-empty>
  155. </div>
  156. </div>
  157. </template>
  158. <route>
  159. {
  160. name: "showdoc",
  161. meta: {
  162. layout: "base"
  163. }
  164. }
  165. </route>
  166. <script setup lang="ts">
  167. import {
  168. type ArticleDetailType,
  169. type ArticleDetailMenuType,
  170. type CategoryItem,
  171. getArticleDetail,
  172. getArticleCount,
  173. getCategoryTree,
  174. // getArticlesByCateId,
  175. getNearArticles,
  176. } from '@/api'
  177. import {
  178. htmlToTree,
  179. createAnchorNames,
  180. dayjs,
  181. findNodeById,
  182. findBreadcrumbPath,
  183. type TreeNode,
  184. } from '@/utils'
  185. import {
  186. NH1,
  187. NH2,
  188. NH4,
  189. NBackTop,
  190. NEmpty,
  191. NButton,
  192. NBreadcrumb,
  193. NBreadcrumbItem,
  194. NTree,
  195. NAnchor,
  196. NAnchorLink,
  197. } from 'naive-ui'
  198. import type { TreeOption } from 'naive-ui'
  199. const route = useRoute()
  200. const router = useRouter()
  201. const selectedKeys = ref([])
  202. const params = route.params as {
  203. id?: number
  204. }
  205. const breadcrumb = ref<CategoryItem[]>([])
  206. console.log('route', route)
  207. const detail = ref<ArticleDetailType | undefined>()
  208. const mainContents = ref<ArticleDetailMenuType[]>([])
  209. const currentCate = ref<CategoryItem | undefined>()
  210. const mainCategories = ref<CategoryItem[]>([])
  211. const defaultExpandedKeys = ref<number[]>([])
  212. const articleNear = ref<ArticleDetailType[]>([])
  213. const isOpenMuen = ref<Boolean>(false)
  214. const toggleOpen = () => {
  215. isOpenMuen.value = !isOpenMuen.value
  216. }
  217. const nodeProps = ({ option }: { option: TreeOption }) => {
  218. return {
  219. async onClick() {
  220. console.log('option', option.id)
  221. router.replace(`/showcate/${option.id}`)
  222. setTimeout(() => {
  223. location.reload()
  224. }, 50)
  225. },
  226. }
  227. }
  228. onMounted(async () => {
  229. setTimeout(() => {
  230. const html = document.querySelector('.content-html')
  231. if (html) {
  232. createAnchorNames(html)
  233. }
  234. }, 1000)
  235. if (params.id) {
  236. if (!isNaN(+params.id)) {
  237. await getArticleCount(+params.id)
  238. }
  239. }
  240. })
  241. watchEffect(() => {
  242. if (params.id) {
  243. if (!isNaN(+params.id)) {
  244. getArticleDetail(+params.id).then(async (data) => {
  245. if (data.data) {
  246. detail.value = data.data
  247. document.title = detail.value.title
  248. mainContents.value = htmlToTree(detail.value.content)
  249. if (detail.value.categoryId) {
  250. const res = await getCategoryTree(detail.value.categoryId)
  251. if (res.data) {
  252. mainCategories.value = res.data as CategoryItem[]
  253. if (mainCategories.value) {
  254. currentCate.value = findNodeById(
  255. [mainCategories.value] as unknown as TreeNode[],
  256. detail.value.categoryId,
  257. ) as unknown as CategoryItem
  258. defaultExpandedKeys.value = [currentCate.value.parentId]
  259. const selectID = currentCate.value.parentId
  260. ? currentCate.value.id
  261. : currentCate.value.children[0].id
  262. selectedKeys.value = [selectID]
  263. breadcrumb.value = findBreadcrumbPath(
  264. [mainCategories.value] as unknown as TreeNode[],
  265. detail.value.categoryId,
  266. )
  267. console.log('breadcrumb', [mainCategories.value], breadcrumb.value)
  268. }
  269. }
  270. }
  271. }
  272. })
  273. getNearArticles(+params.id).then((res) => {
  274. if (res.data) {
  275. articleNear.value = res.data
  276. }
  277. })
  278. }
  279. }
  280. })
  281. const handleToArticle = (article: ArticleDetailType) => {
  282. console.log('article', article)
  283. if (!article) {
  284. return
  285. }
  286. router.replace(`/showdoc/${article.id}`)
  287. setTimeout(() => {
  288. location.reload()
  289. }, 100)
  290. }
  291. const handleBreadcrumb = (bread: TreeNode) => {
  292. router.replace(`/showcate/${bread.id}`)
  293. setTimeout(() => {
  294. location.reload()
  295. }, 50)
  296. }
  297. </script>
  298. <style lang="scss" scoped>
  299. :deep(.content-html img) {
  300. // width: 100% !important;
  301. height: auto;
  302. }
  303. .pages {
  304. position: relative;
  305. padding-bottom: 110px;
  306. }
  307. :deep(.n-back-top) {
  308. // position: absolute;
  309. // bottom: 0;
  310. }
  311. :deep(.left-tree) {
  312. --n-node-content-height: 40px !important;
  313. //--n-node-color-hover: rgba(6,97,201,0.06) !important;
  314. //border-right: 1px solid #e5e7eb;
  315. .n-tree-node-wrapper {
  316. width: 240px;
  317. font-size: 16px;
  318. }
  319. }
  320. .articleNear {
  321. &:hover {
  322. cursor: pointer;
  323. color: #5a5a5a;
  324. }
  325. }
  326. .open-btn {
  327. display: none;
  328. svg {
  329. width: 100%;
  330. height: 100%;
  331. }
  332. }
  333. [is-mobile] {
  334. .breadcrumb {
  335. height: 0.88rem;
  336. padding: 0;
  337. margin: 0;
  338. display: flex;
  339. align-items: center;
  340. justify-content: flex-start;
  341. padding: 0 0.5333rem;
  342. overflow-x: auto;
  343. :deep(.n-breadcrumb) {
  344. ul {
  345. display: flex;
  346. align-items: center;
  347. justify-content: flex-start;
  348. }
  349. }
  350. }
  351. .doc-title {
  352. font-weight: bold;
  353. font-size: 0.3733rem;
  354. color: #000000;
  355. line-height: 0.44rem;
  356. padding: 0.5333rem;
  357. box-sizing: border-box;
  358. margin: 0;
  359. }
  360. .content {
  361. min-height: auto;
  362. }
  363. .waper-content {
  364. display: block;
  365. position: relative;
  366. flex-direction: column;
  367. :deep(.n-back-top) {
  368. right: 0.1333rem !important;
  369. width: 0.8rem !important;
  370. height: 0.8rem !important;
  371. padding: 0 !important;
  372. min-width: auto !important;
  373. .n-base-icon {
  374. font-size: 0.5333rem !important;
  375. }
  376. }
  377. .waper-menu {
  378. position: fixed;
  379. left: 0;
  380. top: 5.0667rem;
  381. z-index: 1;
  382. background: #fff;
  383. width: 6.4rem;
  384. height: 30%;
  385. border-right: 1px #ebebeb solid;
  386. border-top: 1px #ebebeb solid;
  387. border-bottom: 1px #ebebeb solid;
  388. overflow: visible;
  389. transition: all 0.3s;
  390. transform: translateX(-100%);
  391. &.open {
  392. transform: translateX(0);
  393. }
  394. :deep(.left-tree) {
  395. // --n-node-content-height: 40px !important;
  396. // //--n-node-color-hover: rgba(6,97,201,0.06) !important;
  397. // //border-right: 1px solid #e5e7eb;
  398. width: 100%;
  399. height: 100%;
  400. overflow-y: auto;
  401. .n-tree-node-wrapper {
  402. width: 100%;
  403. padding: 0;
  404. }
  405. }
  406. .open-btn {
  407. position: absolute;
  408. right: calc(-0.8rem - 1px);
  409. top: -1px;
  410. width: 0.8rem;
  411. height: 0.8533rem;
  412. border-radius: 0px 0.1067rem 0.1067rem 0px;
  413. border: 1px solid #ebebeb;
  414. display: block;
  415. background: #fff;
  416. z-index: 2;
  417. display: flex;
  418. align-items: center;
  419. justify-content: center;
  420. }
  421. }
  422. .infos {
  423. width: 100%;
  424. padding: 0 0.8rem;
  425. box-sizing: border-box;
  426. margin-bottom: 1.6rem;
  427. .box {
  428. font-weight: 500;
  429. font-size: 0.5333rem !important;
  430. color: #000000;
  431. line-height: 0.8rem;
  432. .time {
  433. font-size: 0.2667rem;
  434. color: #999999;
  435. line-height: 0.3067rem;
  436. }
  437. }
  438. :deep(.content-html) {
  439. padding: 0.8rem 0;
  440. h1 {
  441. font-weight: bold;
  442. font-size: 0.4rem;
  443. color: #000000;
  444. line-height: 0.48rem;
  445. margin: 0.2667rem 0;
  446. }
  447. h2 {
  448. font-weight: bold;
  449. font-size: 0.36rem;
  450. color: #000000;
  451. line-height: 0.48rem;
  452. margin: 0.2667rem 0;
  453. }
  454. h3,
  455. h4,
  456. h5,
  457. h6 {
  458. margin: 0.2667rem 0;
  459. font-weight: bold;
  460. font-size: 0.32rem;
  461. color: #000000;
  462. line-height: 0.48rem;
  463. }
  464. p {
  465. font-weight: 400;
  466. font-size: 0.32rem;
  467. color: #000000;
  468. line-height: 0.48rem;
  469. }
  470. video {
  471. width: 100%;
  472. height: auto;
  473. }
  474. img {
  475. // width: auto;
  476. // height: auto;
  477. max-height: 100%;
  478. max-width: 100%;
  479. }
  480. }
  481. .top-clt {
  482. flex-direction: row;
  483. justify-content: space-between;
  484. padding-top: 0.8rem;
  485. .prev {
  486. font-size: 0.2667rem;
  487. display: flex;
  488. width: 45%;
  489. justify-content: flex-start;
  490. .articleNear {
  491. width: 100%;
  492. font-size: 0.32rem;
  493. color: #999898;
  494. line-height: 0.3733rem;
  495. overflow: hidden;
  496. white-space: nowrap;
  497. text-overflow: ellipsis;
  498. }
  499. span {
  500. font-size: 0.32rem;
  501. color: #999898;
  502. // line-height: 0.3733rem;
  503. text-align: left;
  504. width: 100%;
  505. overflow: hidden;
  506. white-space: nowrap;
  507. text-overflow: ellipsis;
  508. }
  509. }
  510. .next {
  511. font-size: 0.2667rem;
  512. display: flex;
  513. width: 45%;
  514. justify-content: flex-end;
  515. span {
  516. font-size: 0.32rem;
  517. color: #999898;
  518. // line-height: 0.3733rem;
  519. text-align: right;
  520. width: 100%;
  521. overflow: hidden;
  522. white-space: nowrap;
  523. text-overflow: ellipsis;
  524. }
  525. }
  526. }
  527. }
  528. }
  529. .desc-list {
  530. padding: 0 0.8rem 1.6rem;
  531. > div {
  532. padding: 0.8rem 0.4267rem;
  533. min-height: auto;
  534. .ext-size-base {
  535. font-weight: bold;
  536. font-size: 0.3733rem;
  537. color: #000000;
  538. line-height: 0.44rem;
  539. }
  540. .n-anchor {
  541. font-weight: 400;
  542. font-size: 0.32rem;
  543. color: #333333;
  544. line-height: 0.3733rem;
  545. }
  546. }
  547. }
  548. }
  549. </style>